/**
 * 将 秒 转换成 **时**分**秒
 * @param {Object} value 秒
 */
function formatSecondsToTime(value) {
	let secondTime = parseInt(value); // 秒
	let minuteTime = 0; // 分
	let hourTime = 0; // 时
	if (secondTime > 60) {
		//如果秒数大于60，将秒数转换成整数
		//获取分钟，除以60取整，得到整数分钟
		minuteTime = parseInt(secondTime / 60);
		//获取秒数，秒数取余，得到整数秒数
		secondTime = parseInt(secondTime % 60);
		//如果分钟大于60，将分钟转换成小时
		if (minuteTime > 60) {
			//获取小时，获取分钟除以60，得到整数小时
			hourTime = parseInt(minuteTime / 60);
			//获取小时后取余的分，获取分钟除以60取余的分
			minuteTime = parseInt(minuteTime % 60);
		}
	}
	//若秒数是个位数，前面用0补齐
	// secondTime = secondTime < 10 ? "0" + secondTime : secondTime;
	var result = "" + secondTime + "秒";
	if (minuteTime > 0) {
		//若分钟数是个位数，前面用0补齐
		// minuteTime = minuteTime < 10 ? "0" + minuteTime : minuteTime;
		result = "" + minuteTime + "分" + result;
	}
	if (hourTime > 0) {
		//若小时数是个位数，前面用0补齐
		// hourTime = hourTime < 10 ? "0" + hourTime : hourTime;
		result = "" + hourTime + "时" + result;
	}
	console.log("result", result)
	return result;
}

/**
 * 秒转 HH:mm:ss
 * @param {Object} value
 */
function formatSeconds(value) {
	var theTime = parseInt(value); // 秒
	var theTime1 = 0; // 分
	var theTime2 = 0; // 小时
	if (theTime > 60) {
		theTime1 = parseInt(theTime / 60);
		theTime = parseInt(theTime % 60);
		if (theTime1 > 60) {
			theTime2 = parseInt(theTime1 / 60);
			theTime1 = parseInt(theTime1 % 60);
		}
	}

	var result = "" + parseInt(theTime); //秒
	if (10 > theTime > 0) {
		result = "0" + parseInt(theTime); //秒
	} else {
		result = "" + parseInt(theTime); //秒
	}

	if (10 > theTime1 > 0) {
		result = "0" + parseInt(theTime1) + ":" + result; //分，不足两位数，首位补充0，
	} else {
		result = "" + parseInt(theTime1) + ":" + result; //分
	}
	if (theTime2 > 0) {
		result = "" + parseInt(theTime2) + ":" + result; //时
	}
	return result;
}

/**
 * 时间戳转 yyyy-MM-dd HH:mm:ss
 * @param {Object} date 日期
 */
function getTimeByDate(date) {
	// let date = new Date(value * 1000);
	//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM; //月补0
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d; //天补0
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h; //小时补0
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m; //分钟补0
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s; //秒补0
	// return y + '-' + MM + '-' + d; //年月日
	return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; //年月日时分秒
}

/**
 * 时间戳转 yyyy-MM-dd HH:mm:ss
 * @param {Object} timeStamp 时间戳
 */
function formatTimeStamp(timeStamp) {
	// let date = new Date(value * 1000);
	let date = new Date(timeStamp);
	//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM; //月补0
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d; //天补0
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h; //小时补0
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m; //分钟补0
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s; //秒补0
	// return y + '-' + MM + '-' + d; //年月日
	return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; //年月日时分秒
}

/**
 * 获取前7天的日期数组
 */
function getWeekDay() {
	var date = new Date();
	var base = Date.parse(date); // 转换为时间戳
	var year = date.getFullYear(); //获取当前年份
	var mon = date.getMonth() + 1; //获取当前月份
	var day = date.getDate(); //获取当前日
	var oneDay = 24 * 3600 * 1000
	// var daytime = `${year}${mon >= 10 ? mon : '0' + mon}${day >= 10 ? day : '0' + day}`; //今日时间
	// this.$data.daytime = daytime; // 今日时间赋值给变量
	var daytimeArr = []
	daytimeArr.push(day);
	for (var i = 1; i < 7; i++) { //前七天的时间
		var now = new Date(base -= oneDay);
		var myear = now.getFullYear();
		var month = now.getMonth() + 1;
		var mday = now.getDate();
		// daytimeArr.push([myear, month >= 10 ? month : '0' + month, mday >= 10 ? mday : '0' + mday].join('-'))
		daytimeArr.push(mday);
	}
	return daytimeArr
}

/**
 * 获取当前的年月日
 */
function getCurrentYMD() {
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	return `${year}-${month}-${day}`;
}

/**
 * 根据日期获取当前时分的时间戳
 * @param {Object} date 日期
 */
function getHourAndMinuteTimesByDate(date) {
	let h = date.getHours();
	let m = date.getMinutes();
	let times = h * 60 + m;
	return times;
}

/**
 * 根据时分的字符串日期获取时间戳
 * @param {Object} date 日期
 */
function getTimesByHourMinuteStr(timeStr) {
	let array = timeStr.split(":");
	let times = 0;
	if (array.length == 2) {
		times = parseInt(array[0]) * 60 + parseInt(array[1])
	}
	return times;
}

function isBetweenStartAndEndTime(startTimeStr, endTimeStr) {
	let currentTimes = getHourAndMinuteTimesByDate(new Date());
	let startTimes = getTimesByHourMinuteStr(startTimeStr)
	let endTimes = getTimesByHourMinuteStr(endTimeStr)
	console.log("开始时间戳：" + startTimes)
	console.log("当前时间戳：" + currentTimes)
	console.log("结束时间戳：" + endTimes)
	if(startTimes <= currentTimes && currentTimes <= endTimes) {
		return true
	}
	return false
}

function getDateByYearAndWeek(year, week) {
	// let yearFirstDay = year + "-01-01"
	// let times = new Date(yearFirstDay).getTime()
	// let weekDays = (week - 1) * 7 + 1
	// let weekTimes = weekDays * 24 * 60 * 60 * 1000
	// console.log("yearFirstDay ==" + yearFirstDay)
	// console.log("times ==" + times)
	// console.log("weekDays ==" + weekDays)
	// console.log("weekTimes ==" + weekTimes)
	// const date = new Date(times + weekTimes);
	// let year2 = date.getFullYear();
	// let month2 = date.getMonth() + 1;
	// let day2 = date.getDate();
	// month2 = month2 > 9 ? month2 : '0' + month2;
	// day2 = day2 > 9 ? day2 : '0' + day2;
	// return `${year2}-${month2}-${day2}`;
	
	// 创建1月4日的日期（总是在第一周）
	const baseDate = new Date(year, 0, 4);
	
	// 获取1月4日是星期几（0=周日, 1=周一...6=周六）
	const dayOfWeek = baseDate.getDay();
	
	// 计算到上一个周一需要减去的天数
	// ISO: 周一=1, 周日=7
	const daysToMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
	
	// 计算该年第一周的周一
	const firstMonday = new Date(baseDate);
	firstMonday.setDate(baseDate.getDate() - daysToMonday);
	
	// 计算目标周的周一
	const targetDate = new Date(firstMonday);
	targetDate.setDate(firstMonday.getDate() + (week - 1) * 7);
	
	let year2 = targetDate.getFullYear();
	let month2 = targetDate.getMonth() + 1;
	let day2 = targetDate.getDate();
	month2 = month2 > 9 ? month2 : '0' + month2;
	day2 = day2 > 9 ? day2 : '0' + day2;
	return `${year2}-${month2}-${day2}`;
}

export default {
	formatSecondsToTime,
	formatSeconds,
	getTimeByDate,
	formatTimeStamp,
	getWeekDay,
	getCurrentYMD,
	getHourAndMinuteTimesByDate,
	getTimesByHourMinuteStr,
	isBetweenStartAndEndTime,
	getDateByYearAndWeek
}